package com.example.demo.service.impl;

import com.example.demo.controller.bean.Result;
import com.example.demo.dto.AddAdDTO;
import com.example.demo.dto.SelectedStudentRequestDTO;
import com.example.demo.mapper.AdManageMapper;
import com.example.demo.mapper.AdWalletMapper;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.AdManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class AdManageServiceImpl implements AdManageService {
    @Autowired
    private AdManageMapper mapper;

    @Autowired
    private AdWalletMapper walletMapper;

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime lastUpdatedTime = LocalDateTime.now();
    String timeNow = lastUpdatedTime.format(formatter);
    @Override
    public int addAdvertisement(AddAdDTO request, String imageUrl, String detailUrl, int userId) {

        int advertiserId = walletMapper.selectAdvertiser(userId);
        request.setAdvertiserId(advertiserId);
        request.setUserId(userId);

        Ad currentAd = setAd(request,imageUrl,detailUrl);
        int insertAd = mapper.insertAd(currentAd);
        if(insertAd>0 && request.getAdType()==2){
            int adId = currentAd.getAdId();
            AdKey currentAdKey = new AdKey();
            currentAdKey.setAdId(adId);
            currentAdKey.setNativePlace(request.getNativePlace());
            currentAdKey.setCollegeId(request.getCollegeId());
            currentAdKey.setGender(request.getGender());
            if(mapper.insertAdKey(currentAdKey) > 0){
                return adId;
            }
            return 0;
        }
        return 0;

    }

    @Override
    public List<Student> getStudentList(int gender, int nativePlace, int collegeId) {
        System.out.println("学生信息——gender:"+gender+" nativePlace:"+nativePlace+" collegeId:"+collegeId);
        int studentGender = 11;
        Student student = new Student();
        if(gender==1) student.setGender(0);
        if(gender==2) student.setGender(1);  //女
        student.setNativePlace(nativePlace);
        student.setCollegeId(collegeId);
        List<Student> list = mapper.getStudentList(student);
        return list;
    }

    @Override
    public int addStudentAd(int adId, int studentId, double priority) {
        System.out.println("学生广告——adId:"+adId+" studentId:"+studentId+" priority:"+priority);
        AdStudent adStudent = new AdStudent();
        adStudent.setAdId(adId);
        adStudent.setStudentId(studentId);
        adStudent.setPriority(priority);
        return mapper.addStudentAd(adStudent);
    }

    @Override
    public int getTotalStudent(SelectedStudentRequestDTO studentData) {
        int totalNum=0;

        if(studentData.getNativePlace()!=0&&studentData.getCollegeId()!=0&&studentData.getGender()!=0){
            if(studentData.getGender()==1) studentData.setGender(0);
            if(studentData.getGender()==2) studentData.setGender(1);
            totalNum=mapper.getTotalStudent(studentData);
            return totalNum;
        }
        if(studentData.getNativePlace()!=0&&studentData.getCollegeId()!=0&&studentData.getGender()==0){
            totalNum=mapper.getTotalStudent1(studentData);
            return totalNum;
        }
        if (studentData.getNativePlace()!=0&&studentData.getCollegeId()==0&&studentData.getGender()==0){
            totalNum=mapper.getTotalStudent2(studentData);
            return totalNum;
        }
        if (studentData.getNativePlace()!=0&&studentData.getCollegeId()==0&&studentData.getGender()!=0){
            if(studentData.getGender()==1) studentData.setGender(0);
            if(studentData.getGender()==2) studentData.setGender(1);
            totalNum=mapper.getTotalStudent3(studentData);
            return totalNum;
        }
        if (studentData.getNativePlace()==0&&studentData.getCollegeId()!=0&&studentData.getGender()!=0){
            if(studentData.getGender()==1) studentData.setGender(0);
            if(studentData.getGender()==2) studentData.setGender(1);
            totalNum=mapper.getTotalStudent4(studentData);
            return totalNum;
        }
        if (studentData.getNativePlace()==0&&studentData.getCollegeId()!=0&&studentData.getGender()==0){
            totalNum=mapper.getTotalStudent5(studentData);
            return totalNum;
        }
        if (studentData.getNativePlace()==0&&studentData.getCollegeId()==0&&studentData.getGender()!=0){
            if(studentData.getGender()==1) studentData.setGender(0);
            if(studentData.getGender()==2) studentData.setGender(1);
            totalNum=mapper.getTotalStudent6(studentData);
            return totalNum;
        }
        if (studentData.getNativePlace()==0&&studentData.getCollegeId()==0&&studentData.getGender()==0){
            totalNum=0;
        }

        return totalNum;
    }


    private Ad setAd(AddAdDTO request, String imageUrl, String detailUrl) {
        Ad currentAd = new Ad();
        currentAd.setAdvertiserId(request.getAdvertiserId());
        currentAd.setTitle(request.getTitle());
        currentAd.setDescription(request.getDescription());
        currentAd.setImageUrl(imageUrl);
        currentAd.setAdType(request.getAdType());
        currentAd.setAdStatus(1);
        currentAd.setUserId(request.getUserId());
        currentAd.setDetailUrl(detailUrl);
        currentAd.setCreateTime(timeNow);

        List<String> dateRange= request.getDateRange();
        if (dateRange != null && dateRange.size() == 2) {

            DateTimeFormatter inputFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; // 用于解析输入的ISO 8601格式

            LocalDateTime startDate = LocalDateTime.parse(dateRange.get(0), inputFormatter);
            LocalDateTime endDate = LocalDateTime.parse(dateRange.get(1), inputFormatter);

            DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            String formattedStartDate = startDate.format(outputFormatter);
            String formattedEndDate = endDate.format(outputFormatter);

            currentAd.setStartTime(formattedStartDate);
            currentAd.setEndTime(formattedEndDate);

            // 或者直接返回这两个字符串
            System.out.println("Start Date: " + formattedStartDate);
            System.out.println("End Date: " + formattedEndDate);
        } else {
            throw new IllegalArgumentException("Invalid date range. It should contain exactly two elements.");
        }
        return currentAd;
    }
}
